package com.shuaigef.springbootinit.controller;

import com.shuaigef.springbootinit.utils.LoginUserContextHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.shuaigef.springbootinit.utils.PageUtils;
import com.shuaigef.springbootinit.common.PageRequest;
import com.shuaigef.springbootinit.common.PageResult;
import com.shuaigef.springbootinit.common.Result;
import com.shuaigef.springbootinit.model.dto.req.post.*;
import com.shuaigef.springbootinit.model.dto.resp.post.GetPostByIdRespDTO;
import com.shuaigef.springbootinit.model.dto.resp.post.GetPostListRespDTO;
import com.shuaigef.springbootinit.model.dto.resp.post.GetPostSayBySayIdRespDTO;
import com.shuaigef.springbootinit.service.PostService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * 帖子接口
 */
@RestController
@RequestMapping("/post")
@RequiredArgsConstructor
@Slf4j
public class PostController {
    private final PostService postService;

    private final StringRedisTemplate stringRedisTemplate;

    /**
     * 根据用户id获取帖子列表
     * @param requestParam 请求参数
     * @return 返回结果
     */
    @PostMapping("/get-post-by-user")
    public Result<PageResult<GetPostListRespDTO>> getPostByUser(@RequestBody GatPostByUserIdReqDTO requestParam) {
        return Result.success(PageUtils.paginate(postService.getPostByUserId(requestParam.getUserId()),
                requestParam.getCurrent(),
                requestParam.getPageSize()));
    }

    /**
     * 根据用户id获取帖子列表
     * @param requestParam 请求参数
     * @return 返回结果
     */
    @PostMapping("/get-post-by-current-user")
    public Result<PageResult<GetPostListRespDTO>> getPostByCurrentUser(@RequestBody PageRequest requestParam) {
        return Result.success(PageUtils.paginate(postService.getPostByUserId(LoginUserContextHolder.getUserId()),
                requestParam.getCurrent(),
                requestParam.getPageSize()));
    }

    /**
     * 获取帖子列表
     *
     * @param requestParam 请求参数
     * @return 返回结果
     */
    @GetMapping("/list")
    public Result<PageResult<GetPostListRespDTO>> getPostList(GetPostListReqDTO requestParam) {
        return Result.success(PageUtils.paginate(postService.getPostList(requestParam),
                requestParam.getCurrent(),
                requestParam.getPageSize()));
    }

    /**
     * 添加帖子
     *
     * @param requestParam 请求参数
     * @return 返回结果
     */
    @PostMapping(value = "/add", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<Void> addPost(AddPostReqDTO requestParam) {
        return postService.addPost(requestParam);
    }

    /**
     * 删除帖子
     *
     * @param requestParam 请求参数
     * @return 返回结果
     */
    @PostMapping("/delete")
    public Result<Void> deletePost(@RequestBody DeletePostByIdReqDTO requestParam) {
        return postService.deletePost(requestParam);
    }

    /**
     * 修改帖子
     *
     * @param requestParam 请求参数
     * @return 返回结果
     */
    @PostMapping(value = "/update", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<Void> updatePost(UpdatePostByIdReqDTO requestParam) {
        return postService.updatePost(requestParam);
    }

    /**
     * 根据id获取帖子信息
     *
     * @param id 帖子id
     * @return 返回结果
     */
    @GetMapping("/get")
    public Result<GetPostByIdRespDTO> getPost(@RequestParam("id") Integer id) {
        return postService.getPostById(id);
    }

    /**
     * 帖子点赞
     *
     * @param requestParam 请求参数
     * @return 返回结果
     */
    @PostMapping("/like-post")
    public Result<Void> likePost(@RequestBody LikePostReqDTO requestParam) {
        return postService.likePost(requestParam);
    }

    /**
     * 帖子取消点赞
     *
     * @param requestParam 请求参数
     * @return 返回结果
     */
    @PostMapping("/unlike-post")
    public Result<Void> unlikePost(@RequestBody UnLikePostReqDTO requestParam) {
        return postService.unlikePost(requestParam);
    }

    /**
     * 添加评论
     *
     * @param requestParam 请求参数
     * @return 返回结果
     */
    @PostMapping("/say")
    public Result<Void> say(@RequestBody AddPostSayReqDTO requestParam) {
        return postService.addSay(requestParam);
    }

    /**
     * 修改评论
     *
     * @param requestParam 请求参数
     * @return 返回结果
     */
    @PostMapping("/update-say")
    public Result<Void> updateSay(@RequestBody UpdateSayReqDTO requestParam) {
        return postService.updateSay(requestParam);
    }

    /**
     * 删除评论
     *
     * @param requestParam 请求参数
     * @return 返回结果
     */
    @GetMapping("/delete-say")
    public Result<Void> deleteSay(@RequestBody DeletePostSayReqDTO requestParam) {
        return postService.deleteSay(requestParam);
    }

    /**
     * 点赞评论
     *
     * @param requestParam 请求参数
     * @return 操作结果
     */
    @PostMapping("/like")
    public Result<Void> likeSay(@RequestBody LikePostSayReqDTO requestParam) {
        return postService.likeSay(requestParam);
    }

    /**
     * 取消点赞评论
     *
     * @param requestParam 请求参数
     * @return 操作结果
     */
    @PostMapping("/unlike")
    public Result<Void> unlikeSay(@RequestBody UnLikePostSayReqDTO requestParam) {
        return postService.unlikeSay(requestParam);
    }

    /**
     * 根据评论id获取评论信息
     * 帖子id必填 评论id可选
     * 评论id为空的话返回该帖子下所有评论，有评论id的话返回该评论下的所有评论
     *
     * @param requestParam  评论id
     * @param postId 帖子id
     * @return 评论信息
     */
    @GetMapping("/{postId}/say")
    public Result<PageResult<GetPostSayBySayIdRespDTO>> getSays(@PathVariable("postId") Integer postId,
                                                              GetPostSayReqDTO requestParam) {
        return Result.success(PageUtils.paginate(postService.getSays(postId, requestParam),
                requestParam.getCurrent(),
                requestParam.getPageSize()));
    }

    /**
     * 举报帖子
     * @param requestParam 请求参数
     * @return 返回结果
     */
    @PostMapping("/report")
    public Result<Void> report(@RequestBody ReportPostAndSayReqDTO requestParam) {
        return postService.report(requestParam);
    }
} 